package com.ccrfid.rmc6.resource.business.chongqing;

import com.ccrfid.rmc6.appconfig.auth.Secured;
import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.entity.business.chongqing.CQPatrolLocationEntity;
import com.ccrfid.rmc6.entity.business.chongqing.CQPatrolRecordEntity;
import com.ccrfid.rmc6.entity.business.chongqing.CQPatrolRoadEntity;
import com.ccrfid.rmc6.entity.poi.PoiLocationEntity;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.business.chongqing.CQPatrolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.sql.Timestamp;
import java.util.List;

@Secured
@Path("cq/patrol")
@Produces(MediaType.APPLICATION_JSON)
public class CQPatrolResource {

    @Autowired
    private CQPatrolService patrolService;

    //分页查询巡更路线信息
    @GET
    @Path("road/page")
    public Page<CQPatrolRoadEntity> getCQPatrolRoadPage(@QueryParam("roadName") String roadName,
                                                        @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (StringUtils.hasLength(roadName)) {
            criteria.add(new Criterion("roadName", Criterion.Operator.LIKE, "%" + roadName + "%"));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return patrolService.getCQPatrolRoadPage(criteria, pageRequest);
    }

    //获取单个路线信息
    @GET
    @Path("road/{roadId}")
    public CQPatrolRoadEntity getCQPatrolRoadById(@PathParam("roadId") Long roadId) {
        return patrolService.getCQPatrolRoadById(roadId);
    }

    //新增路线信息
    @POST
    @Path("road")
    @Consumes(MediaType.APPLICATION_JSON)
    public CQPatrolRoadEntity createCQPatrolRoad(CQPatrolRoadEntity entity) throws AppException {
        return patrolService.createCQPatrolRoad(entity);
    }

    //更新路线信息
    @PUT
    @Path("road/{roadId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public CQPatrolRoadEntity updateCQPatrolRoad(@PathParam("roadId") Long roadId,
                                                 CQPatrolRoadEntity entity) throws AppException {
        return patrolService.updateCQPatrolRoad(roadId, entity);
    }

    //删除路线信息
    @DELETE
    @Path("road/{roadId}")
    public boolean deleteCQPatrolRoad(@PathParam("roadId") Long roadId) throws AppException {
        return patrolService.deleteCQPatrolRoad(roadId);
    }

    //获取可用于添加的巡更区域列表
    @GET
    @Path("road/free-location/list")
    public List<PoiLocationEntity> getFreePatrolLocationList() {
        return patrolService.getFreePatrolLocationList();
    }

    //获取某个路线的巡更区域列表
    @GET
    @Path("road/{roadId}/location/list")
    public List<CQPatrolLocationEntity> getCQPatrolLocationList(@PathParam("roadId") Long roadId) {
        return patrolService.getCQPatrolLocationList(roadId);
    }

    //为某个路线增加一个巡更区域
    @PUT
    @Path("road/{roadId}/location/{locationId}")
    public CQPatrolLocationEntity createCQPatrolLocation(@PathParam("roadId") Long roadId,
                                                         @PathParam("locationId") Long locationId) throws AppException {
        return patrolService.createCQPatrolLocation(roadId, locationId);
    }

    //为某个路线删除一个巡更区域
    @DELETE
    @Path("road/{roadId}/location/{locationId}")
    public boolean deleteCQPatrolLocation(@PathParam("roadId") Long roadId,
                                          @PathParam("locationId") Long locationId) throws AppException {
        return patrolService.deleteCQPatrolLocation(roadId, locationId);
    }

    //分页查询巡更记录信息
    @GET
    @Path("record/page")
    public Page<CQPatrolRecordEntity> getCQPatrolRecordPage(@QueryParam("patrolRoadId") Long patrolRoadId,
                                                            @QueryParam("patrolRoadName") String patrolRoadName,
                                                            @QueryParam("personnelId") Long personnelId,
                                                            @QueryParam("personnelName") String personnelName,
                                                            @QueryParam("eventType") Integer eventType,
                                                            @QueryParam("eventTypeRemark") String eventTypeRemark,
                                                            @QueryParam("startTime") Timestamp startTime,
                                                            @QueryParam("stopTime") Timestamp stopTime,
                                                            @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (patrolRoadId != null) {
            criteria.add(new Criterion("patrolRoadId", Criterion.Operator.EQ, patrolRoadId));
        }
        if (StringUtils.hasLength(patrolRoadName)) {
            criteria.add(new Criterion("patrolRoadName", Criterion.Operator.LIKE, "%" + patrolRoadName + "%"));
        }
        if (personnelId != null) {
            criteria.add(new Criterion("personnelId", Criterion.Operator.EQ, personnelId));
        }
        if (StringUtils.hasLength(personnelName)) {
            criteria.add(new Criterion("personnelName", Criterion.Operator.LIKE, "%" + personnelName + "%"));
        }
        if (eventType != null) {
            criteria.add(new Criterion("eventType", Criterion.Operator.EQ, eventType));
        }
        if (StringUtils.hasLength(eventTypeRemark)) {
            criteria.add(new Criterion("eventTypeRemark", Criterion.Operator.LIKE, "%" + eventTypeRemark + "%"));
        }
        if (startTime != null) {
            criteria.add(new Criterion("recordTime", Criterion.Operator.GTE, startTime));
        }
        if (stopTime != null) {
            criteria.add(new Criterion("recordTime", Criterion.Operator.LTE, stopTime));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return patrolService.getCQPatrolRecordPage(criteria, pageRequest);
    }
}
